package com.zbcz.erp.activity

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.activity.viewModels
import androidx.core.content.ContextCompat
import androidx.core.widget.addTextChangedListener
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.alibaba.android.arouter.utils.TextUtils
import com.zbcz.erp.R
import com.zbcz.erp.activity.ApplyForSealActivity.Companion.convertDicItem
import com.zbcz.erp.activity.BusinessTripRequestActivity.Companion.TAG
import com.zbcz.erp.adapter.CommonEditFileAdapter
import com.zbcz.erp.adapter.CommonToolsListAdapter
import com.zbcz.erp.adapter.CommonToolsListAdapter.ChooseDataTypeHolder
import com.zbcz.erp.adapter.CommonToolsListAdapter.Companion.CHOOSE_DATA_TYPE
import com.zbcz.erp.adapter.CommonToolsListAdapter.Companion.CHOOSE_DATE_TYPE
import com.zbcz.erp.adapter.CommonToolsListAdapter.Companion.EDIT_TYPE
import com.zbcz.erp.adapter.CommonToolsListAdapter.Companion.REMARK_TYPE
import com.zbcz.erp.adapter.CommonToolsListAdapter.Companion.UPLOAD_FILE_TYPE
import com.zbcz.erp.adapter.CommonToolsListAdapter.UploadFileType
import com.zbcz.erp.adapter.CustomSpinnerAdapter
import com.zbcz.erp.adapter.MyApplyToolsAdapter.Companion.MY_TOOLS_BEAN
import com.zbcz.erp.bean.BusinessAddBean
import com.zbcz.erp.bean.BusinessAddBean.Companion.REPORT_BUSINESS_FIELDS
import com.zbcz.erp.bean.CommonListBean
import com.zbcz.erp.bean.DictItem
import com.zbcz.erp.bean.DoneJobBean
import com.zbcz.erp.bean.FileItem
import com.zbcz.erp.bean.MyToolsBean
import com.zbcz.erp.databinding.ActivityReimbursementFormBinding
import com.zbcz.erp.fragment.CommonListFragment
import com.zbcz.erp.fragment.CommonListFragment.Companion.DONE_JOB_BEAN
import com.zbcz.erp.util.DensityUtils
import com.zbcz.erp.util.REIMBURSEMENT_DOCUMENT
import com.zbcz.erp.util.ToastUtils
import com.zbcz.erp.util.convertDateFormat
import com.zbcz.erp.util.onSingleClick
import com.zbcz.erp.viewmodel.CommonRequestViewModel
import com.zbcz.erp.viewmodel.CommonRequestViewModel.Companion.PAYMENT_TYPE
import java.io.File

/**
 * 报销单
 */
@Route(path = REIMBURSEMENT_DOCUMENT)
class ReimbursementFormActivity : BaseActivity() {

    @Autowired(name = MY_TOOLS_BEAN)
    @JvmField
    var toolsBean: MyToolsBean? = null

    @Autowired(name = DONE_JOB_BEAN)
    @JvmField
    var mJobBean: DoneJobBean? = null

    @JvmField
    @Autowired
    var type: Int = 0

    private lateinit var mBinding: ActivityReimbursementFormBinding

    private val mViewModel: CommonRequestViewModel by viewModels()


    companion object {
        fun createCommonListBeans(): List<CommonListBean> {
            val items = listOf(
                CommonListBean(
                    id = R.id.private_car_public_trip_form,
                    title = "私车公用行程记录表",
                    type = CHOOSE_DATA_TYPE,
                ),
                CommonListBean(id = R.id.happen_date, title = "发生日期", type = CHOOSE_DATE_TYPE),
                CommonListBean(
                    id = R.id.reimbursement_use,
                    title = "报销用途/事项",
                    type = EDIT_TYPE,
                    isNecessary = true
                ),
                CommonListBean(id = R.id.itemized_items, title = "列支项目", type = EDIT_TYPE),
                CommonListBean(
                    id = R.id.budget_number,
                    title = "预算号",
                    type = EDIT_TYPE,
                    isNecessary = true
                ),
                CommonListBean(
                    id = R.id.number_of_receipts,
                    title = "单据张数（张）",
                    type = EDIT_TYPE,
                    isNecessary = true
                ),
                CommonListBean(
                    id = R.id.amount_of_the_general_ticket,
                    title = "普票金额",
                    type = EDIT_TYPE,
                    isNecessary = true
                ),
                CommonListBean(
                    id = R.id.special_ticket_not_include_tax,
                    title = "专票不含税金额（元）",
                    type = EDIT_TYPE,
                    isNecessary = true
                ),
                CommonListBean(
                    id = R.id.amount_of_special_ticket,
                    title = "专票税额（元）",
                    type = EDIT_TYPE,
                    isNecessary = true
                ),
                CommonListBean(
                    id = R.id.payment_amount,
                    title = "付款金额（元）",
                    type = EDIT_TYPE,
                    isNecessary = true
                ),
                CommonListBean(
                    id = R.id.financial_review_of_payment_amount,
                    title = "财务审核付款金额（元）",
                    type = EDIT_TYPE,
                    isNecessary = true
                ),
                CommonListBean(
                    id = R.id.payment_method,
                    title = "付款方式",
                    type = CHOOSE_DATA_TYPE,
                    isNecessary = true
                ),
                CommonListBean(
                    id = R.id.annex,
                    title = "附件",
                    type = UPLOAD_FILE_TYPE,
                    isNecessary = true
                ),
                CommonListBean(id = R.id.contract_number, title = "合同编号", type = EDIT_TYPE),
                CommonListBean(id = R.id.contract_amount, title = "合同金额（元）", type = EDIT_TYPE),
                CommonListBean(
                    id = R.id.beneficiar_unit_name,
                    title = "收款单位名称",
                    type = EDIT_TYPE,
                    isNecessary = true
                ),
                CommonListBean(
                    id = R.id.bank,
                    title = "开户行",
                    type = EDIT_TYPE,
                    isNecessary = true
                ),
                CommonListBean(
                    id = R.id.account,
                    title = "账号",
                    type = EDIT_TYPE,
                    isNecessary = true
                ),
                CommonListBean(id = R.id.remark_type, title = "备注", type = REMARK_TYPE)
            )

            return items
        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityReimbursementFormBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
        ARouter.getInstance().inject(this)
        initViews()
        initUploadInfos()
    }

    private var mAdapter: CommonToolsListAdapter? = null

    private fun initViews() {
        with(mBinding) {
            // 设置顶部边距
            clParent.setPadding(0, DensityUtils.dp2px(this@ReimbursementFormActivity, 48f), 0, 0)
            clTitle.ivCommonBack.onSingleClick { finish() }
            clTitle.tvTitle.text = "报销单"
            mAdapter = CommonToolsListAdapter(isEditAble = toolsBean != null).apply {
                submitList(createCommonListBeans())
            }
            rcList.adapter = mAdapter
            mBinding.tvConfirm.onSingleClick {
                onConfirmClick()
            }
            mBinding.tvCancel.onSingleClick {
                finish()
            }
            mViewModel.getDicItem(PAYMENT_TYPE)
            mViewModel.mPaymentTypeLiveData.observe(this@ReimbursementFormActivity) {
                val holder = mAdapter?.findTargetViewHolder(R.id.payment_method)
                if (holder is ChooseDataTypeHolder) {
                    holder.binding.spSubTitle.adapter =
                        convertDicItem(it?.toMutableList())?.let { it1 ->
                            CustomSpinnerAdapter(
                                this@ReimbursementFormActivity,
                                it1
                            )
                        }
                }

            }
            //Handler().postDelayed({ setInitialValues() }, 2000)
            // 监听提交结果
            mViewModel.requestResult.observe(this@ReimbursementFormActivity) { pair ->
                if (pair.second == true) {
                    ToastUtils.show("提交成功")
                    finish()
                } else {
                    ToastUtils.show(pair.first)
                }
            }
        }
        mBinding.tvCancel.onSingleClick{
            finish()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == ErpWorkProcessActivity.REQUEST_CODE_OPEN_DOCUMENT && resultCode == RESULT_OK) {
            data?.data?.let { fileUri ->
                Log.d(TAG, fileUri.toString())
                val viewHolder = mAdapter?.findTargetViewHolder(R.id.annex)
                if (viewHolder is UploadFileType) {
                    viewHolder.mFileAdapter?.addData(fileUri)
                }
            }
        }
    }

    private fun onConfirmClick() {
        processAllViewHolders()
    }

    private fun processAllViewHolders() {
        // 获取和处理私车公用行程记录表
        val mAddBean = BusinessAddBean().apply {
            id = toolsBean?.id
            name = toolsBean?.name
            procDeTitle = toolsBean?.name
            procDefId = toolsBean?.id
            filedNames = REPORT_BUSINESS_FIELDS
            tableName = BusinessAddBean.EXPENSE_TABLE
        }
        var mTargetFile: File? = null
        val privateCarViewHolder = mAdapter?.findTargetViewHolder(R.id.private_car_public_trip_form)
        if (privateCarViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            // 设置提示或初始值等逻辑
            //privateCarViewHolder.binding.edEditFile.hint = "选择或新建私车公用行程记录表"
            mAddBean.report_business_name = privateCarViewHolder.binding.edEditFile.toString()
        }

        // 获取和处理发生日期
        val happenDateViewHolder = mAdapter?.findTargetViewHolder(R.id.happen_date)
        if (happenDateViewHolder is CommonToolsListAdapter.ChooseDateTypeHolder) {
            // 日期选择处理逻辑
            //happenDateViewHolder.binding.tvSubTitle.text = "2024-08-14"
            mAddBean.report_business_date =
                convertDateFormat(happenDateViewHolder.binding.tvSubTitle.text.toString())
        }

        // 获取和处理报销用途/事项
        val reimbursementUseViewHolder = mAdapter?.findTargetViewHolder(R.id.reimbursement_use)
        if (reimbursementUseViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            // 处理报销用途/事项的逻辑，比如设置初始值或监听输入变化等
            //reimbursementUseViewHolder.binding.edEditFile.setText("默认报销用途")
            mAddBean.report_business_purpose =
                reimbursementUseViewHolder.binding.edEditFile.text.toString()
        }


        // 获取和处理列支项目
        val itemizedItemsViewHolder = mAdapter?.findTargetViewHolder(R.id.itemized_items)
        if (itemizedItemsViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            // 处理列支项目的逻辑
            //itemizedItemsViewHolder.binding.edEditFile.setText("列支项目内容")
            mAddBean.report_business_project =
                itemizedItemsViewHolder.binding.edEditFile.text.toString()
        }

        // 获取和处理预算号
        val budgetNumberViewHolder = mAdapter?.findTargetViewHolder(R.id.budget_number)
        if (budgetNumberViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            // 设置预算号的初始值或逻辑
            mAddBean.report_business_budget_number =
                budgetNumberViewHolder.binding.edEditFile.text.toString()
        }

        // 获取和处理单据张数（张）
        val numberOfReceiptsViewHolder = mAdapter?.findTargetViewHolder(R.id.number_of_receipts)
        if (numberOfReceiptsViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            // 处理单据张数的逻辑
            mAddBean.report_business_number =
                numberOfReceiptsViewHolder.binding.edEditFile.text.toString()
            //TODO
        }

        // 获取和处理普票金额
        val generalTicketAmountViewHolder =
            mAdapter?.findTargetViewHolder(R.id.amount_of_the_general_ticket)
        if (generalTicketAmountViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            // 设置普票金额的初始值或逻辑
            mAddBean.report_business_amount =
                generalTicketAmountViewHolder.binding.edEditFile.text.toString()
            //generalTicketAmountViewHolder.binding.edEditFile.setText("1000.00")
        }

        // 获取和处理专票不含税金额（元）
        val specialTicketNotIncludeTaxViewHolder =
            mAdapter?.findTargetViewHolder(R.id.special_ticket_not_include_tax)
        if (specialTicketNotIncludeTaxViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            // 处理专票不含税金额的逻辑
            //specialTicketNotIncludeTaxViewHolder.binding.edEditFile.setText("800.00")
            mAddBean.report_business_special_amount =
                specialTicketNotIncludeTaxViewHolder.binding.edEditFile.text.toString()
        }

        // 获取和处理专票税额（元）
        val specialTicketAmountViewHolder =
            mAdapter?.findTargetViewHolder(R.id.amount_of_special_ticket)
        if (specialTicketAmountViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            // 设置专票税额的初始值或逻辑
            //specialTicketAmountViewHolder.binding.edEditFile.setText("200.00")
            mAddBean.report_business_special_tax_amount =
                specialTicketAmountViewHolder.binding.edEditFile.text.toString()

        }

        // 获取和处理付款金额（元）
        val paymentAmountViewHolder = mAdapter?.findTargetViewHolder(R.id.payment_amount)
        if (paymentAmountViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            // 设置付款金额的初始值或逻辑
            //paymentAmountViewHolder.binding.edEditFile.setText("1200.00")
            mAddBean.report_business_payment_amount =
                paymentAmountViewHolder.binding.edEditFile.text.toString()
        }

        // 获取和处理财务审核付款金额（元）
        val financialReviewAmountViewHolder =
            mAdapter?.findTargetViewHolder(R.id.financial_review_of_payment_amount)
        if (financialReviewAmountViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            // 处理财务审核付款金额的逻辑
            mAddBean.report_business_financial_audit_amount =
                financialReviewAmountViewHolder.binding.edEditFile.text.toString()
            //financialReviewAmountViewHolder.binding.edEditFile.setText("1200.00")
        }

        // 获取和处理付款方式
        val paymentMethodViewHolder = mAdapter?.findTargetViewHolder(R.id.payment_method)
        if (paymentMethodViewHolder is ChooseDataTypeHolder) {
            // 设置付款方式的逻辑，比如选择列表或默认值等
            //paymentMethodViewHolder.binding.spSubTitle.setSelection(0) // 默认选择第一个选项
            paymentMethodViewHolder.binding.spSubTitle.selectedItem?.let { dict ->
                if (dict is DictItem) {
                    mAddBean.report_business_payment_method = dict.text ?: ""
                    mAddBean.report_business_payment_method_code = dict.value ?: "-1"
                }
            }
        }

        // 获取和处理附件
        val annexViewHolder = mAdapter?.findTargetViewHolder(R.id.annex)
        if (annexViewHolder is UploadFileType) {
            // 处理附件上传的逻辑，比如添加默认文件或初始化空白列表等
            mTargetFile = annexViewHolder.mFileAdapter?.getTargetFile(this)
        }

        // 获取和处理合同编号
        val contractNumberViewHolder = mAdapter?.findTargetViewHolder(R.id.contract_number)
        if (contractNumberViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            // 设置合同编号的初始值或逻辑
            //contractNumberViewHolder.binding.edEditFile.setText("合同编号12345")
            mAddBean.report_business_contract_number =
                contractNumberViewHolder.binding.edEditFile.text.toString()
        }

        // 获取和处理合同金额（元）
        val contractAmountViewHolder = mAdapter?.findTargetViewHolder(R.id.contract_amount)
        if (contractAmountViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            // 设置合同金额的初始值或逻辑
            mAddBean.report_business_contract_amount =
                contractAmountViewHolder.binding.edEditFile.text.toString()
            //contractAmountViewHolder.binding.edEditFile.setText("50000.00")
        }

        // 获取和处理收款单位名称
        val beneficiarUnitNameViewHolder = mAdapter?.findTargetViewHolder(R.id.beneficiar_unit_name)
        if (beneficiarUnitNameViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            // 设置收款单位名称的初始值或逻辑
            mAddBean.report_business_receiving_unit_name =
                beneficiarUnitNameViewHolder.binding.edEditFile.text.toString()
            //beneficiarUnitNameViewHolder.binding.edEditFile.setText("收款单位名称")
        }

        // 获取和处理开户行
        val bankViewHolder = mAdapter?.findTargetViewHolder(R.id.bank)
        if (bankViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            // 设置开户行的初始值或逻辑
            mAddBean.report_business_bank =
                bankViewHolder.binding.edEditFile.text.toString()
            //bankViewHolder.binding.edEditFile.setText("中国银行")
        }

        // 获取和处理账号
        val accountViewHolder = mAdapter?.findTargetViewHolder(R.id.account)
        if (accountViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            // 设置账号的初始值或逻辑
            //accountViewHolder.binding.edEditFile.setText("1234567890")
            mAddBean.report_business_account =
                accountViewHolder.binding.edEditFile.text.toString()
        }

        // 获取和处理备注
        val remarkViewHolder = mAdapter?.findTargetViewHolder(R.id.remark_type)
        if (remarkViewHolder is CommonToolsListAdapter.RemarkTypeHolder) {
            // 设置备注的初始值或逻辑
            mAddBean.report_business_remarks =
                remarkViewHolder.binding.edInputRemark.text.toString()
            //remarkViewHolder.binding.edInputRemark.setText("备注内容")
        }
        mViewModel.createRequest(mAddBean, mTargetFile)
    }

    fun setInitialValues(addBean: BusinessAddBean) {
        // 获取和设置私车公用行程记录表的初始值
        val privateCarViewHolder = mAdapter?.findTargetViewHolder(R.id.private_car_public_trip_form)
        if (privateCarViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            privateCarViewHolder.binding.edEditFile.hint = "选择或新建私车公用行程记录表"
            privateCarViewHolder.binding.edEditFile.setText(addBean.report_business_name)
        }

        // 设置发生日期的初始值
        val happenDateViewHolder = mAdapter?.findTargetViewHolder(R.id.happen_date)
        if (happenDateViewHolder is CommonToolsListAdapter.ChooseDateTypeHolder) {
            happenDateViewHolder.binding.tvSubTitle.text = addBean.report_business_date
            happenDateViewHolder.binding.tvSubTitle.setTextColor(ContextCompat.getColor(this, R.color.black_text))
        }

        // 设置报销用途/事项的初始值
        val reimbursementUseViewHolder = mAdapter?.findTargetViewHolder(R.id.reimbursement_use)
        if (reimbursementUseViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            reimbursementUseViewHolder.binding.edEditFile.setText(addBean.report_business_purpose)
        }

        // 设置列支项目的初始值
        val itemizedItemsViewHolder = mAdapter?.findTargetViewHolder(R.id.itemized_items)
        if (itemizedItemsViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            itemizedItemsViewHolder.binding.edEditFile.setText(addBean.report_business_project)
        }

        // 设置预算号的初始值
        val budgetNumberViewHolder = mAdapter?.findTargetViewHolder(R.id.budget_number)
        if (budgetNumberViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            budgetNumberViewHolder.binding.edEditFile.setText(addBean.report_business_budget_number)
        }

        // 设置单据张数的初始值
        val numberOfReceiptsViewHolder = mAdapter?.findTargetViewHolder(R.id.number_of_receipts)
        if (numberOfReceiptsViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            numberOfReceiptsViewHolder.binding.edEditFile.setText(addBean.report_business_number)
        }

        // 设置普票金额的初始值
        val generalTicketAmountViewHolder =
            mAdapter?.findTargetViewHolder(R.id.amount_of_the_general_ticket)
        if (generalTicketAmountViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            generalTicketAmountViewHolder.binding.edEditFile.setText(addBean.report_business_amount)
        }

        // 设置专票不含税金额的初始值
        val specialTicketNotIncludeTaxViewHolder =
            mAdapter?.findTargetViewHolder(R.id.special_ticket_not_include_tax)
        if (specialTicketNotIncludeTaxViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            specialTicketNotIncludeTaxViewHolder.binding.edEditFile.setText(addBean.report_business_special_amount)
        }

        // 设置专票税额的初始值
        val specialTicketAmountViewHolder =
            mAdapter?.findTargetViewHolder(R.id.amount_of_special_ticket)
        if (specialTicketAmountViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            specialTicketAmountViewHolder.binding.edEditFile.setText(addBean.report_business_special_amount)
        }

        // 设置付款金额的初始值
        val paymentAmountViewHolder = mAdapter?.findTargetViewHolder(R.id.payment_amount)
        if (paymentAmountViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            paymentAmountViewHolder.binding.edEditFile.setText(addBean.report_business_payment_amount)
        }

        // 设置财务审核付款金额的初始值
        val financialReviewAmountViewHolder =
            mAdapter?.findTargetViewHolder(R.id.financial_review_of_payment_amount)
        if (financialReviewAmountViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            financialReviewAmountViewHolder.binding.edEditFile.setText(addBean.report_business_financial_audit_amount)
        }

        // 设置付款方式的初始值
        val paymentMethodViewHolder = mAdapter?.findTargetViewHolder(R.id.payment_method)
        if (paymentMethodViewHolder is ChooseDataTypeHolder) {
            //paymentMethodViewHolder.binding.spSubTitle.setSelection(0) // 默认选择第一个选项
            addBean.report_business_payment_method_code.toIntOrNull()?.let { index ->
                paymentMethodViewHolder.binding.spSubTitle.setSelection(index)
            }
        }

//        // 设置附件的初始值
        val annexViewHolder = mAdapter?.findTargetViewHolder(R.id.annex)
        if (annexViewHolder is UploadFileType) {
            if (type != CommonListFragment.RE_APPLY) {
                if (!TextUtils.isEmpty(addBean?.report_business_file)) {
                    annexViewHolder.mFileAdapter?.submitList(
                        listOf(
                            FileItem(
                                uri = null,
                                type = CommonEditFileAdapter.REMOTE_FILE_TYPE,
                                remoteUrl = addBean?.report_business_file ?: ""
                            )
                        )
                    )
                } else {
//                    tvFiles.visibility = View.GONE
//                    rcFileList.visibility = View.GONE
                }
            } else {
                val mutableFileList =
                    mutableListOf(FileItem(null, CommonEditFileAdapter.ADD_FILE_TYPE))
                if (!TextUtils.isEmpty(addBean?.report_business_file)) {
                    mutableFileList.add(
                        FileItem(
                            uri = null,
                            type = CommonEditFileAdapter.REMOTE_FILE_TYPE,
                            remoteUrl = addBean?.report_business_file ?: "",
                            isDelAble = true
                        )
                    )
                }
                annexViewHolder.mFileAdapter?.submitList(mutableFileList)
            }
            //annexViewHolder.mFileAdapter?.setDefaultFiles() // 根据需要实现默认文件设置方法
        }

        // 设置合同编号的初始值
        val contractNumberViewHolder = mAdapter?.findTargetViewHolder(R.id.contract_number)
        if (contractNumberViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            contractNumberViewHolder.binding.edEditFile.setText(addBean.report_business_contract_number)
        }

        // 设置合同金额的初始值
        val contractAmountViewHolder = mAdapter?.findTargetViewHolder(R.id.contract_amount)
        if (contractAmountViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            contractAmountViewHolder.binding.edEditFile.setText(addBean.report_business_contract_amount)
        }

        // 设置收款单位名称的初始值
        val beneficiarUnitNameViewHolder = mAdapter?.findTargetViewHolder(R.id.beneficiar_unit_name)
        if (beneficiarUnitNameViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            beneficiarUnitNameViewHolder.binding.edEditFile.setText(addBean.report_business_receiving_unit_name)
        }

        // 设置开户行的初始值
        val bankViewHolder = mAdapter?.findTargetViewHolder(R.id.bank)
        if (bankViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            bankViewHolder.binding.edEditFile.setText(addBean.report_business_bank)
        }

        // 设置账号的初始值
        val accountViewHolder = mAdapter?.findTargetViewHolder(R.id.account)
        if (accountViewHolder is CommonToolsListAdapter.EditTypeHolder) {
            accountViewHolder.binding.edEditFile.setText(addBean.report_business_account)
        }

        // 设置备注的初始值
        val remarkViewHolder = mAdapter?.findTargetViewHolder(R.id.remark_type)
        if (remarkViewHolder is CommonToolsListAdapter.RemarkTypeHolder) {
            remarkViewHolder.binding.edInputRemark.setText(addBean.report_business_remarks)
        }
    }


    /**
     * 初始化上传信息
     */
    private fun initUploadInfos() {
        if (type == CommonListFragment.MY_TO_DO_LIST || type == CommonListFragment.MY_DONE_LIST || type == CommonListFragment.MY_APPLY || type == CommonListFragment.RE_APPLY) {
            if (type != CommonListFragment.RE_APPLY) {
                //setFieldsEditable(false)
            }
            mViewModel.queryFormInfo(mJobBean?.tableId ?: "", mJobBean?.tableName ?: "")
            mViewModel.mQueryFormInfo.observe(this) { addBean ->
                //populateFormFields(addBean)
                if (addBean != null) {
                    setInitialValues(addBean)
                }
                if (type == CommonListFragment.RE_APPLY) {
                    if (addBean != null) {
                        //displayReApplyViews(addBean)
                    }
                }
            }

            when (type) {
                CommonListFragment.MY_TO_DO_LIST -> displayToDoViews()
                CommonListFragment.MY_DONE_LIST, CommonListFragment.MY_APPLY -> displayDoneViews()
                //MY_APPLY -> displayReApplyViews()
            }
        }
    }

    /**
     * 显示待办视图
     */
    private fun displayToDoViews() {
        with(mBinding) {
//            tvContentAdvice.visibility = View.VISIBLE
//            edContentAdvice.visibility = View.VISIBLE
//            tvAdviceCount.visibility = View.VISIBLE
            llAdviceList.visibility = View.VISIBLE
            tvCancel.text = "驳回"
            tvConfirm.text = "同意"
            tvCancel.onSingleClick {
                onApplyConfirmClick(
                    mJobBean?.id ?: "", mJobBean?.procInstId ?: "", false
                )
            }
            tvConfirm.onSingleClick {
                onApplyConfirmClick(
                    mJobBean?.id ?: "", mJobBean?.procInstId ?: "", true
                )
            }

            mViewModel.mActPassLiveData.observe(this@ReimbursementFormActivity) { result ->
                handleApprovalResult(result)
            }

            mViewModel.mActBackLiveData.observe(this@ReimbursementFormActivity) { result ->
                handleRejectionResult(result)
            }

            edContentAdvice.addTextChangedListener { text ->
                val textSize = text.toString().length
                tvAdviceCount.text = "$textSize/100"
            }
        }
    }

    /**
     * 显示已完成视图
     */
    private fun displayDoneViews() {
        with(mBinding) {
            clBottomParent.visibility = View.GONE
            nsParent.setPadding(0, 0, 0, 0)
        }
    }


    /**
     * 审批或驳回点击事件
     */
    private fun onApplyConfirmClick(id: String, procInstId: String, isPass: Boolean) {
        mViewModel.actPass(isPass, id, procInstId, mBinding.edContentAdvice.text.toString())
    }

    /**
     * 处理审批结果
     */
    private fun handleApprovalResult(result: Pair<Boolean, String?>) {
        if (result.first) {
            ToastUtils.show("审批通过")
            finish()
        } else {
            ToastUtils.show(result.second ?: "")
        }
    }

    /**
     * 处理驳回结果
     */
    private fun handleRejectionResult(result: Pair<Boolean, String?>) {
        if (result.first) {
            ToastUtils.show("驳回成功")
            finish()
        } else {
            ToastUtils.show(result.second ?: "")
        }
    }
}